package com.jftone.bh.app.action;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.SysAdmin;
import com.jftone.bh.app.service.AdminService;
import org.apache.log4j.Logger;

import org.jftone.util.IData;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.util.IPv4Util;
import org.jftone.util.StringUtil;
import org.jftone.util.VerifyCodeUtil;

@Controller(mapping="/loginAction")
public class LoginAction extends BaseAction {

	private Logger logger = Logger.getLogger(LoginAction.class);
	
	@Autowired
	private AdminService service;
	
	public void execute() throws ActionException{
		this.putRenderInfo("random", Math.random());
		this.render("login.html");
	}
	
	public void image() throws ServletException, IOException {
		HttpServletResponse response = this.getResponse();
		response.setContentType("image/jpeg");
		String verifyCode = VerifyCodeUtil.generateVerifyCode(4);  
		getRequest().getSession().setAttribute(SysConst.IMG_VERIFY_CODE, verifyCode);
		OutputStream os = response.getOutputStream();
        VerifyCodeUtil.outputImage(150, 64, os, verifyCode);
	}

	/**
	 * 登录系统
	 * @throws ActionException
	 */
	public void login() throws ActionException{
		HttpServletRequest request = this.getRequest();
		HttpSession session = request.getSession();
		IData<String, Object> map = getData();
		String adminUser = map.getString("adminUser");
		String password = map.getString("password");
		String verifyCode = map.getString("verifyCode");
		
		if(StringUtil.isBlank(adminUser) ||
				StringUtil.isBlank(password)){
			this.send(4, "");
			return;
		}
		Object imgCode = session.getAttribute(SysConst.IMG_VERIFY_CODE);
		if(null == imgCode || !verifyCode.equalsIgnoreCase(imgCode.toString())){
			this.send(5, "");
			return;
		}
		
		SysAdmin sysAdmin = new SysAdmin();
		sysAdmin.setAdminUser(adminUser);
		sysAdmin.setPassword(password);
		sysAdmin.setLoginIp(IPv4Util.getIpAddr(getRequest()));
		sysAdmin.setLoginTime(new Date());
		try {
			int resultCode = service.login(sysAdmin);
			//如果登录成功，则记录session
			if(resultCode==0) {
				session.setAttribute(SysConst.ADMIN_USER, this.getSessionData(sysAdmin));
			}
			this.send(resultCode, "");
			
		} catch (ComponentException e) {
			logger.error("登录错误", e);
			throw new ActionException("登录错误", e);
		}
	}
	
	
	/**
	 * 注销登录
	 * @throws ActionException
	 */
	public void logout() throws ActionException{
		HttpSession session = this.getRequest().getSession(false);
		if(session == null){
			this.redirect("loginAction.do");
			return;
		}
		session.removeAttribute(SysConst.ADMIN_USER);
		session.removeAttribute(SysConst.RIGHT_CODE);
		session.invalidate();
		this.redirect("loginAction.do");
	}
}
